﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using Binding;
using System.Windows.Input;

namespace UserControlExamples.ViewModels
{
    [Serializable]
    public class CustomerListViewModel : INotifyPropertyChanged, IObserver
    {
        public CustomerListViewModel()
        {
            m_lstCustomerViewModel = new NotifyPropertyCollection<CustomerViewModel>();
            CustomerTypeList = new List<Model.CustomerType>();
        }

        private string m_TestText = "";

        public string TestText
        {
            get { return m_TestText; }
            set
            {
                if (m_TestText != value)
                {
                    m_TestText = value;
                    OnPropertyChanged("TestText");

                }
            }
        }

        public List<Model.CustomerType> CustomerTypeList { get; set; }
        private int m_SelectedCustomerTypeId = 0;
        public int SelectedCustomerTypeId
        {
            get { return m_SelectedCustomerTypeId; }
            set
            {
                if (m_SelectedCustomerTypeId != value)
                {
                    m_SelectedCustomerTypeId = value;
                    OnPropertyChanged("SelectedCustomerTypeId");
                }
            }
        }

        private NotifyPropertyCollection<ViewModels.CustomerViewModel> m_lstCustomerViewModel;
        public NotifyPropertyCollection<ViewModels.CustomerViewModel> ListCustomerViewModel
        {
            get { return m_lstCustomerViewModel; }
            set
            {
                if (m_lstCustomerViewModel != value)
                {
                    m_lstCustomerViewModel = value;
                    m_lstCustomerViewModel.PropertyChanged += new PropertyChangedEventHandler(OnPropertyChanged);
                    foreach (CustomerViewModel oModel in m_lstCustomerViewModel)
                        oModel.RegisterInterest(this);
                }
            }
        }


        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        protected virtual void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(sender, e);
            }
        }



        public void NotifyObserver(object oMessage)
        {
        }

        public ICommand ChangeCustomerType
        {
            get
            {
                return new ChangeCustomerType();
            }
        }


    }

    #region Commands
    public class ChangeCustomerType : ICommand
    {
        #region ICommand Members

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            BinderBase target = (BinderBase)parameter;
            CustomerListViewModel model = (CustomerListViewModel)target.DataContext;

        }

        #endregion
    }
    #endregion
}